<template>
  <div>
    <el-form :model="formData" :rules="formRules" label-width="120px">
      <el-form-item label="节点 id" prop="id">
        <el-input v-model="formData.id" />
      </el-form-item>
      <el-form-item label="节点名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="节点描述" prop="documentation">
        <el-input v-model="formData.documentation" />
      </el-form-item>
      <el-form-item label="执行监听器" prop="executionListener">
        <el-badge :value="executionListenerLength">
          <el-button @click="dialogName = 'executionListenerDialog'">编辑</el-button>
        </el-badge>
      </el-form-item>
      <el-form-item label="跳转条件" prop="conditionExpression">
        <el-input v-model="formData.conditionExpression" />
      </el-form-item>
      <el-form-item label="跳过表达式" prop="skipExpression">
        <el-input v-model="formData.skipExpression" />
      </el-form-item>
    </el-form>
    <executionListenerDialog
      v-if="dialogName === 'executionListenerDialog'"
      :element="element"
      :modeler="modeler"
      @close="finishExecutionListener"
    />
  </div>
</template>

<script setup>
  import { ref, watch, defineProps } from 'vue';
  import mixinPanel from '../../common/mixinPanel';
  import mixinExecutionListener from '../../common/mixinExecutionListener';
  import { StrUtil } from '@/utils/StrUtil';
  import { commonParse, conditionExpressionParse } from '../../common/parseElement';

  const props = defineProps({
    modeler: {
      type: Object,
      required: true,
    },
    element: {
      type: Object,
      required: true,
    },
    categorys: {
      type: Array,
      default: () => [],
    },
  });

  const formData = ref({});
  const formRules = ref({
    id: [{ required: true, message: 'Id 不能为空' }],
  });

  const { updateProperties } = mixinPanel(props.modeler, props.element, formData);
  const { executionListenerDialog, executionListenerLength, dialogName, finishExecutionListener } =
    mixinExecutionListener(props.element);

  watch(
    () => formData.value.conditionExpression,
    (val) => {
      if (StrUtil.isNotBlank(val)) {
        const newCondition = props.modeler
          .get('moddle')
          .create('bpmn:FormalExpression', { body: val });
        updateProperties({ conditionExpression: newCondition });
      }
    },
  );

  watch(
    () => formData.value.skipExpression,
    (val) => {
      if (StrUtil.isNotBlank(val)) {
        updateProperties({ 'flowable:skipExpression': val });
      } else {
        delete props.element.businessObject.$attrs[`flowable:skipExpression`];
      }
    },
  );

  function computedExecutionListenerLength() {
    executionListenerLength.value =
      props.element.businessObject.extensionElements?.values?.filter(
        (item) => item.$type === 'flowable:ExecutionListener',
      ).length ?? 0;
  }

  function init() {
    let cache = commonParse(props.element);
    cache = conditionExpressionParse(cache);
    formData.value = cache;
    computedExecutionListenerLength();
  }

  init();
</script>

<style scoped lang="scss"></style>
